package com.itheima.stargraph.service.impl;

import com.itheima.stargraph.pojo.UserResult;
import com.itheima.stargraph.mapper.UserResultMapper;
import com.itheima.stargraph.service.IUserResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.stargraph.utils.UserThreadlocalUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 生产的结果信息表 服务实现类
 * </p>
 *
 * @author mr.wu
 * @since 2025-06-12
 */
@Service
public class UserResultServiceImpl extends ServiceImpl<UserResultMapper, UserResult> implements IUserResultService {


    /**
     * 批量保存生图结果
     *
     * @param userId
     * @param urls
     */
    @Override
    public void saveBatch(Long userId, List<String> urls) {

        List<UserResult> userResultList = urls.stream().map(x -> {
            return UserResult
                    .builder()
                    .userId(userId) //用户id
                    .url(x) //图片URL
                    .collect(0)//未收藏
                    .build();
        }).collect(Collectors.toList());

        boolean result = this.saveBatch(userResultList);

        if(!result) {
            throw new RuntimeException("批量保存生图结果失败");
        }
    }

    @Override
    public List<UserResult> resultList() {
        List<UserResult> userResultList = this.lambdaQuery()
                .eq(UserResult::getUserId, UserThreadlocalUtils.getUser().getId())
                .orderByDesc(UserResult::getCreatedTime)
                .list();
        return userResultList;
    }
}
